package cn.csust.net2.manager.server.service.impl;
import java.util.List;
import java.util.Map.Entry;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.csust.net2.manager.client.module.ByGrade;
import cn.csust.net2.manager.server.dao.RoleDAO;
import cn.csust.net2.manager.server.dao.UserDAO;
import cn.csust.net2.manager.server.remote.RemoveServiceBean;
import cn.csust.net2.manager.shared.exception.BeyondAuthorityException;
import cn.csust.net2.manager.shared.po.Authority;
import cn.csust.net2.manager.shared.po.PO;
import cn.csust.net2.manager.shared.po.Role;
import cn.csust.net2.manager.shared.service.AuthorizationService;
import cn.csust.net2.manager.shared.util.Constant;
import cn.csust.net2.manager.shared.util.ServiceConstant;
import cn.csust.net2.manager.shared.util.Utils;
import cn.csust.net2.manager.shared.vo.GridConfig;

import com.extjs.gxt.ui.client.data.BasePagingLoadResult;
import com.extjs.gxt.ui.client.data.PagingLoadConfig;

/** 
 * @author 韩忠金
 * @version 创建时间：2012-1-30 上午10:42:53 
 * 类说明 
 */
@Service(value = ServiceConstant.SERVICE_NAME_AUTHORIZATION)
public class AuthorizationServiceImpl extends BaseServiceImpl implements AuthorizationService ,RemoveServiceBean{
	
	@Resource(name = Constant.DAO_NAME_ROLE)
	private RoleDAO roleDAO;
	
	public RoleDAO getRoleDAO() {
		return roleDAO;
	}
	public void setRoleDAO(RoleDAO roleDAO) {
		this.roleDAO = roleDAO;
	}
	@Resource(name = Constant.DAO_NAME_USER)
	private UserDAO userDAO;
	public UserDAO getUserDAO() {
		return userDAO;
	}
	public void setUserDAO(UserDAO userDAO) {
		this.userDAO = userDAO;
	}
	/**
	 * 重写BaSE中的方法使ExpanGrid复用性更高
	 */
	@Transactional
	@Override
	public <T extends PO>  BasePagingLoadResult<T> findAll(String cla1,PagingLoadConfig config)throws BeyondAuthorityException {
		StringBuffer hql = new StringBuffer(" from User c where 1=1 ");
		GridConfig gc = config.get(ByGrade.GRID_FILTER);
		if(gc!=null){
			Set<Entry<String, Object>> ex = gc.entrySet();
			for(Entry<String, Object> entry:ex){
				String key = entry.getKey();
				String value = (String) entry.getValue();
				if(!value.equals(""))
					hql.append("  and c."+Utils.getOrg(key)+" like '%"+value+"%'");
			}
		}
		 return userDAO.findAllByPaging(config,hql.toString());
	}
	@Transactional
	@Override
	public List<Role> loadRole(Long userId) {
		// TODO Auto-generated method stub
		List<Role> list = this.getSqlMapper().loadRole(userId);
		return list;
	}
	@Transactional
	@Override
	public List<Role> loadRoles() {
		// TODO Auto-generated method stub
		
		return roleDAO.findAll();
	}
	@Transactional
	@Override
	public  List<Role> addRole(Long userId, int roleId) {
		// TODO Auto-generated method stub
		this.getSqlMapper().addRole(userId, roleId);
		return loadRole(userId) ;
	}
	@Transactional
	@Override
	public List<Authority> loadAuthorityById(int roleID) {
		// TODO Auto-generated method stub
		return this.getSqlMapper().findAuthoritiesByRoleId(roleID);
	}
	@Transactional
	@Override
	public  List<Role> deleteRole(Long userId, int roleId) {
		// TODO Auto-generated method stub
		this.getSqlMapper().deleteRole(userId, roleId);
		return loadRole(userId) ;
	}
}
